package com.offcn.job;

import com.offcn.utils.RedisConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.Schedules;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Date;
import java.util.Set;

/**
 * @Author: wy
 * @Date: 2022/8/31 15:32
 */
@Component
public class CleanImgJob {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //清理立即照片的方法
    @Scheduled(cron = "* 0/10 * * * ? ")
    public void task() {

        //定时清理垃圾图片
        SetOperations<String, String> opsForSet = stringRedisTemplate.opsForSet();

        //求两个的差级
        Set<String> cleanImg = opsForSet.difference(RedisConstant.SETMEAL_PIC_UPLOAD, RedisConstant.SETMEAL_PIC_DB);

        if (cleanImg != null && cleanImg.size() > 0) {
            //进行删除
            for (String name : cleanImg) {
                File file = new File("/Users/lazy-bay/Desktop/filename/" + name);

                file.delete();

                //并且删除redis 里面的 数据
                opsForSet.remove(RedisConstant.SETMEAL_PIC_UPLOAD, name);
            }
        }
        System.out.println("图片清理成功"+new Date());

    }
}
